﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using FX.CC.Andon.Entity.Service;
using FX.CC.Andon.Business.Service;
using FX.CC.Andon.Entity;
using FX.CC.Andon.Entity.Version;
using FX.CC.Andon.Business.Version;
using FX.CC.Andon.Entity.BasicDataManage;
using FX.CC.Andon.Business.BasicDataManage;

namespace FX.CC.Andon.WebSite.Service
{
    /// <summary>
    /// Summary description for KanbanDataSvc
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class KanbanDataSvc : System.Web.Services.WebService
    {
        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod]
        public DateTime GetServerTime()
        {
            return DateTime.Now;
        }

        [WebMethod]
        public EquipmentEntity GetEquipmentByCode(string eqCode)
        {
            return new EquipmentBusiness().GetByCode(eqCode);
        }

        [WebMethod]
        public PrdTaskWrapper[] GetTaskList(Guid eqID, DateTime bizDate) {
            return new KanbanBiz().GetTaskList(eqID, bizDate).ToArray();
        }

        [WebMethod]
        public string[] GetNotice()
        {
            return new KanbanBiz().GetNotice().ToArray();
        }

        [WebMethod]
        public VersionRsp GetLatestVersion(int clientType)
        {
            bool isExists;
            var ver = new VersionBiz().GetLatestVersion(clientType, out isExists);
            VersionRsp rtObj = new VersionRsp();
            rtObj.VersionObj = ver;
            rtObj.IsExists = isExists;
            return rtObj;
        }

        [WebMethod]
        public CEFile[] GetFiles(Guid versionID)
        {
            return new FileBiz().GetFiles(versionID).ToArray();
        }

        [WebMethod]
        public bool UpdateLoadConfig(Guid sessionID, Guid[] listeningLine, string[] listeningErrors)
        {
            bool ok = true;
            var lineList = new List<KeyValuePairEx<Guid, Guid>>();
            foreach(var wsid in listeningLine){
                lineList.Add(new KeyValuePairEx<Guid,Guid>{ Key = sessionID, Value = wsid });
            }

            var errList = new List<KeyValuePairEx<Guid, string>>();
            foreach (var err in listeningErrors) {
                errList.Add(new KeyValuePairEx<Guid, string> { Key = sessionID, Value = err });
            }

            try
            {
                var biz = new KeyValueBiz();
                biz.DeleteListeningLine(sessionID);
                biz.DeleteListeningError(sessionID);
                ok = biz.AddListeningLine(lineList);
                if (ok)
                {
                    biz.AddListeningError(errList);
                }
                return ok;
            }
            catch {
                throw;
            }
        }

        [WebMethod]
        public KanbanWrapper[] GetEvent (Guid sessionID, DateTime bizDate){
            var set = new KanbanBiz().GetEvent(sessionID, bizDate).ToArray();
            return set;
        }

        [WebMethod]
        public TaskKanbanWrapper[] GetTaskListBySession(Guid sessionID, DateTime bizDate) {
            var set = new KanbanBiz().GetTaskListBySession(sessionID, bizDate).ToArray();
            return set;
        }

        [WebMethod]
        public KeyValuePairEx<Guid, string>[] GetAllWorkShop()
        {
            return new KeyValueBiz().GetAllWorkShop().ToArray();
        }

        [WebMethod]
        public KeyValuePairEx<Guid, string>[] GetAllLine()
        {
            return new KeyValueBiz().GetAllLine().ToArray();
        }

        [WebMethod]
        public KeyValuePairEx<string, string>[] GetAllEType()
        {
            return new KeyValueBiz().GetAllEType().ToArray();
        }

    }
}
